package com.yang.edu;

/**
 * @Description: sql会话类, 负责调用动态代理, 生成代理类传输出去
 *              然后还负责得到SQL语句
 * @author: Yang Qiang
 * @create: 2021-11-09 21:09
 */
public class YangSqlSession {
    // 负责配置的类
    private YangConfiguration yangConfiguration;
    // 负责执行的类(调用数据库)
    private YangExecutor yangExecutor;

    public YangSqlSession() {
    }

    public YangSqlSession(YangConfiguration yangConfiguration, YangExecutor yangExecutor) {
        this.yangConfiguration = yangConfiguration;
        this.yangExecutor = yangExecutor;
    }

    /**
     * 传进来一个mapper接口, 然后返回有个接口的代理类
     * @param mapperClass
     * @param <T>
     * @return
     */
    public <T> T getMapper(Class mapperClass){
        return yangConfiguration.getMapper(mapperClass, this);
    }

    /**
     * 执行SQL语句
     * @param statementId
     * @param arg
     * @return
     */
    public Object selectOne(String statementId, Object arg) {
        // 根据类名.方法名从配置文件获取对应的SQL语句
        String sql = yangConfiguration.getSQLMap().getString(statementId);
        return yangExecutor.selectById(sql, arg);
    }
}
